package cn.csg.iotplatform.module.system.controller.admin.userproject;

import cn.csg.iotplatform.framework.common.pojo.CommonResult;
import cn.csg.iotplatform.module.system.controller.admin.userproject.vo.UserProjectRespVO;
import cn.csg.iotplatform.module.system.controller.admin.userproject.vo.UserProjectSaveReqVO;
import cn.csg.iotplatform.module.system.service.userproject.UserProjectService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

import static cn.csg.iotplatform.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 用户&项目设备授权关系")
@RestController
@RequestMapping("/system/user-project")
@Validated
public class UserProjectController {

    @Resource
    private UserProjectService userProjectService;

    @PostMapping("/save")
    @Operation(summary = "保存用户&项目设备授权关系")
    @PreAuthorize("@ss.hasPermission('system:user-project:create')")
    public CommonResult<Boolean> createUserProject(@Valid @RequestBody UserProjectSaveReqVO createReqVO) {
        userProjectService.createUserProject(createReqVO);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得用户&项目设备授权关系")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:user-project:query')")
    public CommonResult<UserProjectRespVO> getUserProject(@RequestParam("userId") Long userId) {
        return success(userProjectService.getUserProject(userId));
    }

}